ASP.NET Cookies

Cookies are small pieces of text, to be used by the website for cookies only to be stored on the client's computer. This allows web applications to save information to the user, and then reuse it on every page if necessary. Here's an example where users have the option of background color:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Cookies</title>
</head>
<body runat="server" id="BodyTag">
    <form id="form1" runat="server">
    <asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged">
        <asp:ListItem value="White" selected="True">Select color...</asp:ListItem>
        <asp:ListItem value="Red">Red</asp:ListItem>
        <asp:ListItem value="Green">Green</asp:ListItem>
        <asp:ListItem value="Blue">Blue</asp:ListItem>
    </asp:DropDownList>
    </form>
</body>
</html>

The page has a dropdown list control, which automatically posts after each new item is selected, it has 3 different colors, except for the default, which is only white once a new item is selected , The ColorSelector_IndexChanged method is removed, which is from our CodeBehind file:


using System;
using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Cookies["BackgroundColor"] != null)
        {
            ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value;
            BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        }
    }

    protected void ColorSelector_IndexChanged(object sender, EventArgs e)
    {
        BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        HttpCookie cookie = new HttpCookie("BackgroundColor");
        cookie.Value = ColorSelector.SelectedValue;
        cookie.Expires = DateTime.Now.AddHours(1);
        Response.SetCookie(cookie);
    }
}

Ok, two different parts are explained here. First, the page_load method, which is called on each page request. Here we check a cookie to tell us which background color should be used. If we search for it, then we set the list of body drop to follow, as well as the background color of the page, only by accessing the style of the body tag.

After that we have the ColorSelector_IndexChanged method, which every time the user chooses a new color. Here we set the background color of the page, and then we make a cookie, which is the value for us. We allow it to expire after one hour, and after that we set the setcisk method by calling the reaction object.

Try running the example, and set a color Now close the browser, and start it again You will see that the choice of colors is left, and it will be saved for one hour. However, anything prevents you from saving time for more time. Just add a big value for the end date, or set a full price for it